package com.example.bingobox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.TodoBox;
import java.util.List;

public class SearchTodoBoxAdapter extends RecyclerView.Adapter<SearchTodoBoxAdapter.ViewHolder> {
    private Context context;
    private List<TodoBox> todoBoxes;
    private OnTodoBoxActionListener listener;

    public interface OnTodoBoxActionListener {
        void onEditClick(TodoBox todoBox);
        void onDeleteClick(TodoBox todoBox);
    }

    public SearchTodoBoxAdapter(Context context, List<TodoBox> todoBoxes, OnTodoBoxActionListener listener) {
        this.context = context;
        this.todoBoxes = todoBoxes;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_todobox, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TodoBox todoBox = todoBoxes.get(position);
        
        // 设置任务盒名称和描述
        holder.nameText.setText(todoBox.getName());
        if (todoBox.getDescription() != null && !todoBox.getDescription().isEmpty()) {
            holder.descText.setText(todoBox.getDescription());
            holder.descText.setVisibility(View.VISIBLE);
        } else {
            holder.descText.setVisibility(View.GONE);
        }

        // 设置编辑按钮点击事件
        holder.btnEdit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEditClick(todoBox);
            }
        });

        // 设置删除按钮点击事件
        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeleteClick(todoBox);
            }
        });

        // 隐藏不需要的按钮和容器
        holder.expandButton.setVisibility(View.GONE);
        holder.btnDraw.setVisibility(View.GONE);
        holder.detailsContainer.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return todoBoxes.size();
    }

    public void updateTodoBoxes(List<TodoBox> newTodoBoxes) {
        this.todoBoxes = newTodoBoxes;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        TextView descText;
        ImageButton expandButton;
        ImageButton btnEdit;
        ImageButton btnDelete;
        ImageButton btnDraw;
        View detailsContainer;

        ViewHolder(View view) {
            super(view);
            nameText = view.findViewById(R.id.text_todobox_name);
            descText = view.findViewById(R.id.text_todobox_description);
            expandButton = view.findViewById(R.id.btn_expand);
            btnEdit = view.findViewById(R.id.btn_edit);
            btnDelete = view.findViewById(R.id.btn_delete);
            btnDraw = view.findViewById(R.id.btn_draw);
            detailsContainer = view.findViewById(R.id.layout_details_container);
        }
    }
} 